<template>
    <el-container>
      <el-header>
          <span><img height="60px" src="../assets/logo.png" alt=""/> 电商后台管理系统</span>
          <div class="top_nav_btns">
            <el-button type='info' @click="logout">退出系统</el-button>
          </div>
      </el-header>
      <el-container>
        <el-aside :width="collapsFlag?'65px':'200px'">
          <div class="el-menu-collaps-btn" @click="changeMenuCollaps"><i :class="collapsFlag?'el-icon-s-unfold':'el-icon-s-fold'"></i></div>
          <el-menu :default-active="defaultActive"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse="collapsFlag"
          :collapse-transition='false'
          unique-opened
          router>
            <el-submenu :index="item.path" :key='item.id' v-for='item in menus'>
              <template slot="title">
                <i :class="menusIcon[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item :index='subItem.path' :key='subItem.id' v-for="subItem in item.children">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main><router-view></router-view></el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {
  data () {
    return {
      menus: [],
      menusIcon: {
        125: 'el-icon-user',
        103: 'el-icon-connection',
        101: 'el-icon-shopping-bag-1',
        102: 'el-icon-c-scale-to-original',
        145: 'el-icon-data-line'
      },
      defaultActive: '',
      collapsFlag: false
    }
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenus () {
      const { data: res } = await this.axios.get('/private/v1/menus')
      if (res && res.data) {
        this.menus = res.data
      }
    },
    changeMenuCollaps () {
      this.collapsFlag = !this.collapsFlag
    },
    getActiveMenu () {
      let path = this.$route.path
      path = path.substring(path.lastIndexOf('/'))
      this.defaultActive = path.replace('/', '')
      console.log(path)
    }
  },
  created () {
    this.getMenus()
    this.getActiveMenu()
  }
}
</script>

<style lang="less" scoped>
.el-header{
    background-color: rgb(40, 40, 40);
    color: rgb(255, 255, 255);
    text-align: center;
    line-height: 60px;
    padding-left: 0px;
    display: flex;
    justify-content: space-between;
    span{
      vertical-align: middle;
    }
    img{
      vertical-align: middle;
    }
    .top_nav_btns{
      height: 40px;
    }
  }

.el-aside {
  background-color: #333744;
  color: #fff;
  height: 100%;
  .el-menu-collaps-btn {
    background-color: rgb(71, 70, 70);
    i{
      color: rgb(146, 141, 141);
      position: relative;
      left: 50%;
      transform: translate(-50%);
    }
  }
}
.el-menu {
  border-right: 1px solid #333744;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  height: 100%;
}

#app > .el-container {
  margin-bottom: 40px;
  height: 100%;
  .el-container{
    height: 100%;
  }
}
</style>
